/**
 request.js  练习测试
 */
var request = require('request');

function main() {
    get();
}

main();

function get() {
    request('https://passport.csdn.net/login?code=public', function (error, response, body) {
        console.log('error:', error); // Print the error if one occurred
        console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received
        console.dir(response); // Print the response status code if a response was received
        console.log('body:', body); // Print the HTML for the Google homepage.
    });
}
//application/json
function postjson(url,requestData) {
    request({
        url: url,
        method: "POST",
        json: true,
        headers: {
            "content-type": "application/json",
        },
        body: JSON.stringify(requestData)
    }, function(error, response, body) {
        if (!error && response.statusCode == 200) {
        }
    });

}
//application/x-www-form-urlencoded
function postx(url,requestData) {
    request.post({url:'http://service.com/upload', form:{key:'value'}}, function(error, response, body) {
        if (!error && response.statusCode == 200) {
        }
    })

}
// POST multipart/form-data
function postfrom(url,requestData) {
    var formData = {
        // Pass a simple key-value pair
        my_field: 'my_value',
        // Pass data via Buffers
        my_buffer: new Buffer([1, 2, 3]),
        // Pass data via Streams
        my_file: fs.createReadStream(__dirname + '/unicycle.jpg'),
    };
    request.post({url:'http://service.com/upload', formData: formData}, function (error, response, body) {
        if (!error && response.statusCode == 200) {
        }
    })

}
